﻿using System;
using System.Collections;
using System.Linq;
using System.Web;

namespace ESAM_Assignment_3
{
    public class CookieClass
    {
        #region Attributes
        private string username;
        private string password;
        #endregion

        #region Properties | GET and SET methods
        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Generic class constructor
        /// </summary>
        public CookieClass()
        {
        }
        #endregion

        /// <summary>
        /// Write user's details to a cookie to be used in ChangePassword.aspx
        /// </summary>
        /// <param name="Context"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public void WriteCookie(HttpContext Context, string username, string password)
        {
            // Create an instance of HttpCookie class
            HttpCookie UserCookie = new HttpCookie("ESAM_ASSIGNMENT_3");

            // Store the user's details
            UserCookie["ESAM_USERNAME"] = username;
            UserCookie["ESAM_PASSWORD"] = password;

            // Set the cookie expiration date
            UserCookie.Expires = DateTime.Now.AddDays(7);

            // Write the cookie to the client computer
            Context.Response.Cookies.Add(UserCookie);
        }

        /// <summary>
        /// In ChangePassword.aspx, the user's details are retrieved from the cookie.
        /// </summary>
        /// <param name="Context"></param>
        public void ReadCookie(HttpContext Context)
        {
            // Retrieve the cookie into a cookie object
            HttpCookie UserCookie = Context.Request.Cookies["ESAM_ASSIGNMENT_3"];

            // Write Username and Password values into the cookie object
            if (UserCookie != null)
            {
                username = UserCookie["ESAM_USERNAME"];
                password = UserCookie["ESAM_PASSWORD"];
            }
            else
            {
                username = "";
                password = "";
            }
        }
    }
}